package com.ruoyi.project.cms.knowledge.controller;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.knowledge.domain.Project;
import com.ruoyi.project.cms.knowledge.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 公司:sxt
 * 作者: MR.Z
 * 类名: ProjectController
 * 描述:
 * 时间: 2020-05-19
 */

@Controller
@RequestMapping("/knowledge/project")
public class ProjectController extends BaseController {

    private static String prefix = "/cms/knowledge";

    @Autowired
    private ProjectService projectService;

    @GetMapping("")
    public String project() {
        return prefix + "/project";
    }

    /*@RequiresPermissions("system:role:list")*/
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Project project) {
        startPage();
       /* List<Project> list = new ArrayList<>();

        Project p1 = new Project();
        p1.setPid(1);
        p1.setPname("真皮沙发");
        p1.setPicture("/picture");
        p1.setStarttime("2020-05-01");
        p1.setClosetime("2020-05-02");
        p1.setPdesc("室内软装");


        list.add(p1);*/

        List<Project> list = projectService.findMore(project);


        return getDataTable(list);
    }





    /**
     * 查看详情
     */
    @GetMapping("/detail/{pid}")
    public String detail(@PathVariable("pid") String pid,ModelMap mmap)
    {
        /*Project p2 = new Project();
        p2.setPname("好工程");
        p2.setDrawing("tuzhi");
        p2.setMaterials("上等材料");
        p2.setDetails("指定优等工程");*/



        mmap.put("project", projectService.selectById(pid));
        return prefix + "/detail";
    }

    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }


    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Project project)
    {
        System.out.println(project);
        return toAjax(projectService.add(project));
    }

    /**
     * 新增
     */
   /* @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated Project project)
    {
        if (UserConstants.ROLE_NAME_NOT_UNIQUE.equals(projectService.checkProjectNameUnique(project.getPname())))
        {
            return error("新增项目'" + project.getPname() + "'失败，项目名称已存在");
        }

        return toAjax(projectService.add(project));

    }*/


   /* @PostMapping("/checkProjectNameUnique")
    @ResponseBody
    public String checkTypeNameUnique(String pname) {

        System.out.println(pname);
        return projectService.checkProjectNameUnique(pname);
    }*/

    /**
     * 修改项目
     */
    @GetMapping("/edit/{pid}")
    public String edit(@PathVariable("pid") String pid, ModelMap mmap)
    {
        mmap.put("project", projectService.selectById(pid));
        mmap.put("tid", projectService.selectTBP(pid));
        return prefix + "/edit";
    }



    /**
     * 修改保存项目
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Project project)
    {
        /*projectService.checkUserAllowed(user);
        if (UserConstants.USER_PHONE_NOT_UNIQUE.equals(userService.checkPhoneUnique(user)))
        {
            return error("修改用户'" + user.getLoginName() + "'失败，手机号码已存在");
        }*/
        System.out.println("返回的数据是："+project);

        return toAjax(projectService.updateProject(project));
    }

    /**
     * 删除项目
     */
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        System.out.println(ids);
        return toAjax(projectService.deleteProjectByIds(ids));
    }
}
